/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DB;

import java.io.Serializable;
import java.util.Date;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.xml.bind.annotation.XmlRootElement;

/**
 *
 * @author weidas
 */
@Entity
@Table(name = "PrivateMessage")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "PrivateMessage.findAll", query = "SELECT p FROM PrivateMessage p"),
    @NamedQuery(name = "PrivateMessage.findById", query = "SELECT p FROM PrivateMessage p WHERE p.privateMessagePK.id = :id"),
    @NamedQuery(name = "PrivateMessage.findByMessage", query = "SELECT p FROM PrivateMessage p WHERE p.message = :message"),
    @NamedQuery(name = "PrivateMessage.findByTimesent", query = "SELECT p FROM PrivateMessage p WHERE p.timesent = :timesent"),
    @NamedQuery(name = "PrivateMessage.findBySenderId", query = "SELECT p FROM PrivateMessage p WHERE p.privateMessagePK.senderId = :senderId"),
    @NamedQuery(name = "PrivateMessage.findByReceiverId", query = "SELECT p FROM PrivateMessage p WHERE p.privateMessagePK.receiverId = :receiverId")})
public class PrivateMessage implements Serializable {
    private static final long serialVersionUID = 1L;
    @EmbeddedId
    protected PrivateMessagePK privateMessagePK;
    @Column(name = "message")
    private String message;
    @Basic(optional = false)
    @Column(name = "timesent")
    @Temporal(TemporalType.TIMESTAMP)
    private Date timesent;
    @JoinColumn(name = "receiver_id", referencedColumnName = "id", insertable = false, updatable = false)
    @ManyToOne(optional = false, fetch = FetchType.EAGER)
    private User user;
    @JoinColumn(name = "sender_id", referencedColumnName = "id", insertable = false, updatable = false)
    @ManyToOne(optional = false, fetch = FetchType.EAGER)
    private User user1;

    public PrivateMessage() {
    }

    public PrivateMessage(PrivateMessagePK privateMessagePK) {
        this.privateMessagePK = privateMessagePK;
    }

    public PrivateMessage(PrivateMessagePK privateMessagePK, Date timesent) {
        this.privateMessagePK = privateMessagePK;
        this.timesent = timesent;
    }

    public PrivateMessage(int id, int senderId, int receiverId) {
        this.privateMessagePK = new PrivateMessagePK(id, senderId, receiverId);
    }

    public PrivateMessagePK getPrivateMessagePK() {
        return privateMessagePK;
    }

    public void setPrivateMessagePK(PrivateMessagePK privateMessagePK) {
        this.privateMessagePK = privateMessagePK;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Date getTimesent() {
        return timesent;
    }

    public void setTimesent(Date timesent) {
        this.timesent = timesent;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public User getUser1() {
        return user1;
    }

    public void setUser1(User user1) {
        this.user1 = user1;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (privateMessagePK != null ? privateMessagePK.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof PrivateMessage)) {
            return false;
        }
        PrivateMessage other = (PrivateMessage) object;
        if ((this.privateMessagePK == null && other.privateMessagePK != null) || (this.privateMessagePK != null && !this.privateMessagePK.equals(other.privateMessagePK))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "DB.PrivateMessage[ privateMessagePK=" + privateMessagePK + " ]";
    }
    
}
